<template>
    <el-container>
        <!-- 头部区域 -->
        <el-header>
            <div>
                <img src="../assets/heima.png" alt="">
                <span>电商后台管理系统</span>
            </div>
            <!-- 返回按钮 -->
            <el-button type="" @click="logout">返回</el-button>
        </el-header>
        <el-container>
            <!-- 侧边栏 -->
            <el-aside :width="isCollapse ? '64px':'200px'">

                <div class="toggle-button" @click="toggleCollapse">|||</div>

                <!-- 一级目录 -->
                <!-- unique-opened 保持一个子目录展开 -->
                <!-- collapse-transition 关闭折叠动画 -->
                <!-- collapse 是否折叠 -->
                <!-- router 将index作为path进行路由跳转 -->
                <el-menu background-color="#333744" text-color="#fff" active-text-color="#409eff" unique-opened :collapse="isCollapse" router :collapse-transition="false" :default-active="'/'+activePath">

                    <el-submenu :index="item.id+''" v-for="item in menulist" :key="item.id">
                        <template slot="title">
                                <!-- 图标 -->
                            <i :class="iconsObj[item.id]"></i>
                                <!-- 文本 -->
                            <span>{{item.authName}}</span>
                        </template>
                        <!-- 二级目录 -->
                        <el-menu-item :index="'/'+sItem.path" v-for="sItem in item.children" :key="sItem.id" @click="saveNavState(sItem.path)">
                            <template slot="title">
                                <!-- 图标 -->
                                <i class="el-icon-menu"></i>
                                <!-- 文本 -->
                                <span>{{sItem.authName}}</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                </el-menu>

            </el-aside>
            <el-main>
                <!-- 路由占位符 -->
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>

<style lang="less" scoped>
    .el-container{
        height: 100%;
        .el-header{
            background-color: #373d41;
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding-left: 0;
            >div{
                color: #fff;
                font-size: 20px;
                display: flex;
                align-items: center;
                span{
                    padding-left: 15px;
                }
            }
        }
        .el-aside{
            background-color: #333744;
            .el-menu{
                border: none;
            }
        }
        .el-main{
            background-color: #eaedf1;
        }
        .iconfont{
            margin-right: 10px;
        }
        .toggle-button{
            background-color: #4a5064;
            color: #fff;
            font-size: 10px;
            line-height: 24px;
            text-align: center;
            cursor: pointer;
            // 竖线之间的间距
            letter-spacing: 0.2em;
        }
    }
</style>
<script>
export default {
    data(){
        return {
            // 菜单数据
            menulist:[],
            iconsObj:{
                '125':'iconfont icon-user',
                '103':'iconfont icon-tijikongjian',
                '101':'iconfont icon-shangpin',
                '102':'iconfont icon-danju',
                '145':'iconfont icon-baobiao'
            },
            // 是否折叠
            isCollapse:false,
            // 被激活的链接地址
            activePath:''
        }
    },
    created(){
        // 获取菜单数据
        this.getMenuList()
        this.activePath=window.sessionStorage.getItem('activePath')
    },
    methods: {
        logout(){
            window.sessionStorage.clear()
            this.$router.push('login')
        },
        async getMenuList(){
            // 将获取的数据解构赋值并重命名为res
            const {data:res}=await this.$http.get('menus')

            // console.log(res)
            if(res.meta.status!==200)return this.$message.error(res.meta.msg)
            this.menulist=res.data
        },
        // 点击按钮切换菜单的折叠和展开
        toggleCollapse(){
            this.isCollapse=!this.isCollapse
        },
        // 保存链接的激活状态
        saveNavState(activePath){
            window.sessionStorage.setItem('activePath',activePath)
            this.activePath=activePath
        }
    },
}
</script>